package PROYECTO2.src;
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import PROYECTO2.src.Frames_Minds.Mind;
import PROYECTO2.src.Frames_Minds.PanelCylons;
import PROYECTO2.src.Naves.Spaceship;
import PROYECTO2.src.Naves.Jugador.Viper;
import PROYECTO2.src.Naves.Jugador.Rhino;
import PROYECTO2.src.Naves.Jugador.Raptor;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class GUIGame extends JFrame {
	
	private JPanel first;
	private JButton back;
	private JLabel labelViper;
	private JLabel labelRhino;
	private JLabel labelRaptor;
	private JLabel viperStatics, rhinoStatics, raptorStatics;
	private JButton viper;
	private JButton  rhino;
	private JButton raptor;
	private JButton quit;
	private JButton newGame;
	private JPanel main;
	private JPanel space;
	private Mind mind;
	private Game game;
	private JPanel bar;
	private JButton comeBackMenu;
	
	

	 public GUIGame() {
		 
		initGUI();
    }

    public static void main(String[] args) {
        new GUIGame();
        
    }
	    
    //inicializa el panel del juego
    
    private void initGame(int select){
    	
    	space = new JPanel();
    	
    	game = new Game();
		getContentPane().add(space, BorderLayout.CENTER);
		space.setBackground(new java.awt.Color(0,0,0));
		
		
		
		mind = new Mind(game,select);
		space.add(mind);
		mind.setLayout(null);
		mind.setPreferredSize(new java.awt.Dimension(800, 520));
		
		// Barra de estado
	
		bar = new JPanel();
		space.add(bar);
		bar.setLayout(null);
		bar.setPreferredSize(new java.awt.Dimension(800, 50));
		bar.setBackground(new java.awt.Color(25,100,25));
		
		mind.setBar(bar);
	
        // boton volver al menu
		
		comeBackMenu = new JButton("Salir al Menu");
		comeBackMenu.addActionListener(new OyenteComeBack());
		comeBackMenu.setBounds(25, 11, 150, 25);
		comeBackMenu.setForeground(new java.awt.Color(0,255,0));
		comeBackMenu.setBackground(new java.awt.Color(0,0,0));
		comeBackMenu.setBorder(BorderFactory.createCompoundBorder(null,null));
		comeBackMenu.setFont(new java.awt.Font("Segoe UI",0,20));
		bar.add(comeBackMenu);
		
		
        // Mente de los enemigos
		
        PanelCylons panelCylons = new PanelCylons(game);
        mind.add(panelCylons);
        panelCylons.setBounds(0, 0, 800, 600);
		
        // Establece las Mentes al mapa
        
        game.setMind(mind);
        game.setMindCylons(panelCylons.getMindCylons());
		
        
        // Inicia el hilo de los Enemigos
      
        panelCylons.getMindCylons().start();
        
	}
	    
  //inicializa el primer panel para iniciar o salir del juego
    
    private void initGUI() {
		
    	initfirstPanel();
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(800, 600);
        setLocationRelativeTo(null);
        setTitle("Chaos Wind");
        setResizable(false);
        setVisible(true);
        this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
				closeGame();
			}
		});
		
	}
	
	private void initfirstPanel(){
		first = new JPanel();
		getContentPane().add(first);
		first.setLayout(null);
		first.setBackground(new java.awt.Color(0,0,0));
		{
			newGame = new JButton();
			first.add(newGame);
			newGame.setText("JUEGO NUEVO");
			newGame.setBounds(400 - 288/2, 90, 288, 50);
			newGame.setForeground(new java.awt.Color(0,255,0));
			newGame.setBackground(new java.awt.Color(0,0,0));
			newGame.setBorder(BorderFactory.createCompoundBorder(null,null));
			newGame.setFont(new java.awt.Font("Segoe UI",0,20));
			newGame.addActionListener(new OyenteNewGame());
		}
		{
			quit = new JButton();
			first.add(quit);
			quit.setText("SALIR");
			quit.setBounds(400 - 288/2, 177, 288, 50);
			quit.setBackground(new java.awt.Color(0,0,0));
			quit.setForeground(new java.awt.Color(0,255,0));
			quit.setPreferredSize(new java.awt.Dimension(14, 7));
			quit.setFont(new java.awt.Font("Segoe UI",0,20));
			quit.setBorder(BorderFactory.createCompoundBorder(null, null));
			quit.addActionListener(new OyenteQuit());
		}
	}
	
	//inicializa el segundi panel para seleccionar tipo de nave
	
	private void initMain(){
		main = new JPanel();
		getContentPane().add(main);
		main.setLayout(null);
		main.setBackground(new java.awt.Color(0,0,0));
		{
			viper = new JButton();
			main.add(viper);
			viper.setText("Viper");
			viper.setBounds(400 - 288/2, 88, 288, 64);
			viper.setBackground(new java.awt.Color(0,0,0));
			viper.setFont(new java.awt.Font("Segoe UI",0,20));
			viper.setForeground(new java.awt.Color(0,255,0));
			viper.addActionListener(new OyenteViper());
		}
		{
			rhino = new JButton();
			main.add(rhino);
			rhino.setText("Rhino");
			rhino.setBounds(256, 188, 288, 64);
			rhino.setFont(new java.awt.Font("Segoe UI",0,20));
			rhino.setForeground(new java.awt.Color(0,255,0));
			rhino.setBackground(new java.awt.Color(0,0,0));
			rhino.addActionListener(new OyenteRhino());
		}
		{
			raptor = new JButton();
			main.add(raptor);
			raptor.setText("Raptor");
			raptor.setBounds(400 - 288/2, 288, 288, 64);
			raptor.setBackground(new java.awt.Color(0,0,0));
			raptor.setForeground(new java.awt.Color(0,255,0));
			raptor.setFont(new java.awt.Font("Segoe UI",0,20));
			raptor.addActionListener(new OyenteRaptor());
		}
		{
			labelViper = new JLabel();
			main.add(labelViper);
			ImageIcon icon = new ImageIcon(Spaceship.class.getClassLoader().getResource("PROYECTO2/src/img/Jugador/Viper.gif"));
			icon=new ImageIcon(icon.getImage().getScaledInstance(50, 50, Image.SCALE_DEFAULT));
			labelViper.setIcon(icon);
			labelViper.setBounds(175 + icon.getIconWidth() / 2, 88, icon.getIconWidth(), 64);
			labelViper.setBackground(new java.awt.Color(155,155,155));
			labelViper.setOpaque(true);
			
			viperStatics = new JLabel();
			main.add(viperStatics);
			viperStatics.setFont(new java.awt.Font("Segoe UI",0,9));
			viperStatics.setForeground(new java.awt.Color(0,255,0));
			viperStatics.setText(Viper.getStatistics());
			viperStatics.setBounds(80, 88, 164, 64);
			viperStatics.setBackground(new java.awt.Color(155,155,155));
			viperStatics.setOpaque(true);
			
		}
		{
			labelRhino = new JLabel();
			main.add(labelRhino);
			ImageIcon icon = new ImageIcon(Spaceship.class.getClassLoader().getResource("PROYECTO2/src/img/Jugador/Rhino.gif"));
			icon=new ImageIcon(icon.getImage().getScaledInstance(50, 50, Image.SCALE_DEFAULT));
			labelRhino.setIcon(icon);
			labelRhino.setBounds(175 + icon.getIconWidth() / 2, 188, 50, 64);
			labelRhino.setOpaque(true);
			labelRhino.setBackground(new java.awt.Color(155,155,155));
			
			rhinoStatics = new JLabel();
			main.add(rhinoStatics);
			rhinoStatics.setFont(new java.awt.Font("Segoe UI",0,9));
			rhinoStatics.setForeground(new java.awt.Color(0,255,0));
			rhinoStatics.setText(Rhino.getEstadisticas());
			rhinoStatics.setBounds(80, 188, 164, 64);
			rhinoStatics.setBackground(new java.awt.Color(155,155,155));
			rhinoStatics.setOpaque(true);
			
		}
		{
			labelRaptor = new JLabel();
			main.add(labelRaptor);
			ImageIcon icon = new ImageIcon(Spaceship.class.getClassLoader().getResource("PROYECTO2/src/img/Jugador/Raptor.gif"));
			icon=new ImageIcon(icon.getImage().getScaledInstance(50, 50, Image.SCALE_DEFAULT));
			labelRaptor.setIcon(icon);
			labelRaptor.setBounds(175, 288, icon.getIconWidth(), 64);
			labelRaptor.setBackground(new java.awt.Color(155,155,155));
			labelRaptor.setOpaque(true);
			
			raptorStatics = new JLabel();
			main.add(raptorStatics);
			raptorStatics.setFont(new java.awt.Font("Segoe UI",0,9));
			raptorStatics.setForeground(new java.awt.Color(0,255,0));
			raptorStatics.setText(Raptor.getEstadisticas());
			raptorStatics.setBounds(80, 288, 164, 64);
			raptorStatics.setBackground(new java.awt.Color(155,155,155));
			raptorStatics.setOpaque(true);
		}
		{
			back = new JButton();
			main.add(back);
			ImageIcon ii = new ImageIcon(Spaceship.class.getClassLoader().getResource("PROYECTO2/src/img/Menu_barras/back.png"));
			back.setIcon(new ImageIcon(ii.getImage().getScaledInstance(61,44,Image.SCALE_DEFAULT)));
			
			back.setBounds(70, 461, 61, 44);
			back.setBackground(new java.awt.Color(0,0,0));
			back.setFont(new java.awt.Font("Segoe UI",0,20));
			back.setForeground(new java.awt.Color(0,255,0));
			back.addActionListener(new OyenteBack());
		}
	
}
	
	private class OyenteQuit implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			
            int verd;
            
            JOptionPane dialogo = new JOptionPane();
            
            verd = dialogo.showConfirmDialog(null, "Desea salir del juego?", "Atencion",JOptionPane.YES_NO_OPTION);
			
            if(verd == 0){
            	System.exit(0);
            }
            
		}
	}
		
	private class OyenteNewGame implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			
            first.setVisible(false);
            setVisible(false);
            initMain();
            setVisible(true);
            
		}
	}
	
	private class OyenteViper implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			main.setVisible(false);
			setVisible(false);
			initGame(1);
			setVisible(true);
			
		}
	}
	
	private class OyenteRhino implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			main.setVisible(false);
			setVisible(false);
			initGame(2);
			setVisible(true);
            
		}
	}
	
	private class OyenteRaptor implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			main.setVisible(false);
			setVisible(false);
			initGame(3);
			setVisible(true);
           
		}
	}
	
	private class OyenteBack implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			
			main.setVisible(false);
			initfirstPanel();
		}
	}
	
	private class OyenteComeBack implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			
            int verd;
            
            JOptionPane dialogo = new JOptionPane();
            
            verd = dialogo.showConfirmDialog(null, "Desea volver al menu principal?", "Atencion",JOptionPane.YES_NO_OPTION);
			
            if(verd == 0){
            	game.stop();
            	space.setVisible(false);
    			setVisible(false);
    			initMain();
    			setVisible(true);
            }
            
		}
	}
	
	
	private void closeGame() {
		this.dispose();
		System.exit(0);
		
	}
	
}
